﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Telerik.Web.UI;

namespace EXAM.Admin.Page_Admin
{
    public partial class QnA : System.Web.UI.Page
    {
        string sErr = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadUserCbo();
            }
        }

        private void LoadUserCbo()
        {
            BUS.UserControl ctr = new BUS.UserControl();
            cboUser.DataSource = ctr.GetAll_User(ref sErr);
            cboUser.DataBind();
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            RefreshForm("");
        }
        private void RefreshForm(string val)
        {
            hdID.Value = val;
            txtQuest.Content = val;
            //cboParent.Text = val;
            txtAnswer.Content = val;
            cboUser.SelectedValue = null;
            hdIDUser.Value = val;
            cboStatus.SelectedValue = "N";
            cboType.SelectedValue = "Q&A";
            lbErr.Text = "";
            txtQuest.Content = "";
            dpAnswer.SelectedDate = null;
            dpCreate.SelectedDate = null;
            txtNotes.Text = val;
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            DTO.QnAInfo usrInf = new DTO.QnAInfo();
            usrInf = GetDataFromForm(usrInf);

            BUS.QnAControl ctr = new BUS.QnAControl();
            if (hdID.Value != "" && hdID.Value != "-1")
            {
                sErr = ctr.Update(usrInf);
                if (sErr != "")
                    lbErr.Text = sErr;
                else
                    lbErr.Text = "Update successfull";
            }
            else
            {
                hdID.Value = ctr.Add(usrInf, ref sErr).ToString();
                if (sErr != "")
                    lbErr.Text = sErr;
                else
                    lbErr.Text = "Insert successfull";
            }
            if (sErr == "")
            {
                dgvList.Rebind();
            }
        }

        private DTO.QnAInfo GetDataFromForm(DTO.QnAInfo usrInf)
        {
            if (hdID.Value != "" && hdID.Value != "-1")
                usrInf.ID = Convert.ToInt32(hdID.Value);
            usrInf.Question = txtQuest.Content;
            usrInf.Answer = txtAnswer.Content;
            usrInf.IDUserPost = Convert.ToInt64(cboUser.SelectedValue);

            usrInf.Status = cboStatus.SelectedValue;
            usrInf.Type = cboType.SelectedValue;
            usrInf.Notes = txtNotes.Text;
            usrInf.CreateTime = dpCreate.SelectedDate == null ? new DateTime(1900, 1, 1) : dpCreate.SelectedDate.Value;
            usrInf.AnswerTime = dpAnswer.SelectedDate == null ? new DateTime(1900, 1, 1) : dpAnswer.SelectedDate.Value;

            return usrInf;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (GridItem item in dgvList.SelectedItems)
            {
                if (item.ItemIndex >= 0)
                {
                    string id = item.OwnerTableView.DataKeyValues[item.ItemIndex]["ID"].ToString();
                    BUS.QnAControl ctr = new BUS.QnAControl();
                    sErr = ctr.Delete(Convert.ToInt32(id));
                }
            }
            if (sErr == "")
            {
                dgvList.Rebind();
            }
        }

        protected void dgvList_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            BUS.QnAControl ctr = new BUS.QnAControl();
            dgvList.DataSource = ctr.GetAll(ref  sErr);
        }

        protected void dgvList_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "SELECTED")
            {
                string id = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString();
                BUS.QnAControl ctr = new BUS.QnAControl();
                SetDataToForm(ctr.Get(Convert.ToInt32(id), ref sErr));
                lbErr.Text = "";
            }
        }

        private void SetDataToForm(DTO.QnAInfo QnAInfo)
        {
            if (QnAInfo.ID != -1)
                hdID.Value = QnAInfo.ID.ToString();
            txtQuest.Content = QnAInfo.Question;
            txtAnswer.Content = QnAInfo.Answer;
            hdIDUser.Value = QnAInfo.IDUserPost.ToString();
            BUS.UserControl ctr = new BUS.UserControl();
            DTO.UserInfo inf = ctr.Get_User(QnAInfo.IDUserPost, ref sErr);
            //txtUser.Text = inf.Username;
            cboStatus.SelectedValue = QnAInfo.Status;
            cboType.SelectedValue = QnAInfo.Type;
            if (QnAInfo.CreateTime == new DateTime(1900, 1, 1))
                dpCreate.SelectedDate = null;
            else dpCreate.SelectedDate = QnAInfo.CreateTime;
            if (QnAInfo.AnswerTime == new DateTime(1900, 1, 1))
                dpAnswer.SelectedDate = null;

            else dpAnswer.SelectedDate = QnAInfo.AnswerTime;
            cboUser.SelectedValue = QnAInfo.IDUserPost.ToString();
            txtNotes.Text = QnAInfo.Notes;
        }

        protected void cboUser_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
        {
            string name = ((DataRowView)e.Item.DataItem)["Lastname"].ToString() + " " + ((DataRowView)e.Item.DataItem)["Firstname"].ToString();
            string position = ((DataRowView)e.Item.DataItem)["ANAL_U0"].ToString() == "" ? "" : "," + ((DataRowView)e.Item.DataItem)["ANAL_U0"].ToString() + " Pos.";
            string depart = ((DataRowView)e.Item.DataItem)["ANAL_U1"].ToString() == "" ? "" : "," + ((DataRowView)e.Item.DataItem)["ANAL_U1"].ToString() + " Dep.";
            string pro = ((DataRowView)e.Item.DataItem)["ANAL_U2"].ToString() == "" ? "" : "," + ((DataRowView)e.Item.DataItem)["ANAL_U2"].ToString();
            e.Item.Text = name +
                position +
                depart +
                pro;
            e.Item.Value = ((DataRowView)e.Item.DataItem)["ID"].ToString();
        }


    }
}
